<!--
 * @Author: quit
 * @Date: 2021-11-16 18:02:50
 * @LastEditTime: 2021-12-16 11:54:15
 * @Descripttion: 阈值管理
-->
<template>
  <div id="ThresholdManagement-content">
    <div class="app-container">
      <el-card>
        <el-row class="row1">
          <el-divider content-position="left">{{ $t('divider.divider1') }}</el-divider>
          <div class="search">
            <el-form :inline="true" :model="formInline" class="demo-form-inline">
              <el-form-item :label="'阈值名称'">
                <el-input v-model="formInline.thresholdname" :placeholder="$t('input.searchPlaceholder')" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-zoom-in" @click="getThresholdList">{{ $t('button.search') }}</el-button>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-refresh-right" @click="refresh">{{ '重置' }}</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-row>
        <el-row>
          <el-button type="primary" size="mini" icon="el-icon-circle-plus-outline" @click="addItemsBtn">{{ $t('button.addItems') }}</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="batchRemove">{{ $t('button.batchRemove') }}</el-button>
        </el-row>
        <el-row>
          <el-table ref="tabel" v-loading="loading" :data="tableData" :empty-text="$t('table.emptyText')" style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column type="selection" min-width="40" fixed="left" />
            <el-table-column prop="thresholdid" label="ID" width="50" fixed="left" />
            <el-table-column prop="thresholdname" :label="'阈值名称'" min-width="120" fixed="left" />
            <el-table-column prop="thresholddescribe" :label="'阈值描述'" min-width="100" />
            <el-table-column header-align="center" align="center" fixed="right" :label="$t('table.operable')" width="200">
              <template slot-scope="scope">
                <el-button type="warning" size="mini" @click="handleEdit(scope.row)">{{ $t('button.edit') }}</el-button>
                <el-button type="danger" size="mini" @click="handleDelete(scope.row)">{{ $t('button.delete') }}</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="block">
            <el-pagination
              :current-page="pages.page"
              :page-sizes="[1, 5, 10, 15]"
              :page-size="pages.size"
              layout="prev, pager, next, total, sizes, jumper"
              :total="pages.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-row>
      </el-card>
    </div>
    <!-- 添加\修改弹出框 -->
    <el-dialog :title="dislogTitle" width="30%" :visible.sync="dialogFormVisible" @close="dialogClose">
      <el-form ref="dialogFrom" label-position="right" :model="dialogFrom" label-width="100px" class="dialogFrom">
        <el-row>
          <el-form-item label="阈值名">
            <el-input v-model="dialogFrom.thresholdname" autocomplete="off" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="文字描述">
            <el-input v-model="dialogFrom.thresholddescribe" type="textarea" :rows="3" autocomplete="off" />
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="dialogFormState === 0" type="primary" @click="dialogSubmit">确 定</el-button>
        <el-button v-if="dialogFormState === 1" type="primary" @click="dialogEditSubmit">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getThresholdPageList, addThresholdItme, editThresholdItme, deleteThreshold } from '@/api/BmRequest'

export default {
  components: {},
  data() {
    return {
      formInline: { thresholdname: null },
      loading: false,
      tableData: [],
      pages: { page: 1, size: 10, total: 0 },
      dislogTitle: null,
      dialogFormVisible: false,
      dialogFormState: null,
      dialogFrom: { thresholdname: null, thresholddescribe: null },
      checkedItmes: []
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  created() {
    this.getThresholdList()
  }, // 生命周期 - 创建完成（可以访问当前this实例）
  mounted() {}, // 生命周期 - 挂载完成（可以访问DOM元素）
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
  // 方法集合
  methods: {
    // 获取阈值列表数据
    async getThresholdList() {
      const obj = {
        thresholdmanager: { thresholdname: this.formInline.thresholdname },
        pages: { page: this.pages.page, size: this.pages.size }
      }
      this.loading = true
      try {
        const {
          data: { records, total, size, current }
        } = await getThresholdPageList(obj)
        this.tableData = records
        this.pages.total = total
        this.pages.size = size
        this.pages.page = current
      } catch (error) {
        console.log(error)
      } finally {
        this.loading = false
      }
    },

    // 重置
    refresh() {
      this.formInline = { thresholdname: null }
      this.pages = { page: 1, size: 10 }
      this.getThresholdList()
    },

    // 表格多选事件
    async handleSelectionChange(val) {
      const arr = []
      val.forEach((item, i) => {
        arr.push(item.thresholdid)
      })
      this.checkedItmes = arr
    },

    // 添加按钮
    addItemsBtn() {
      this.dislogTitle = '添加阈值信息'
      this.dialogFormState = 0
      this.dialogFormVisible = true
    },

    // 批量删除
    async batchRemove() {
      if (this.checkedItmes.length > 0) {
        this.$confirm('此操作将删除所选项, 是否继续?', '确认操作', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async() => {
            if (this.checkedItmes.length === this.tableData.length) {
              if (this.pages.page > 1) {
                this.pages.page--
              } else {
                this.pages.page = 1
              }
            }
            const { message } = await deleteThreshold(this.checkedItmes)
            await this.getThresholdList()
            this.$message.success({ message })
          })
          .catch(error => {
            console.log(error)
            this.$refs.tabel.clearSelection()
          })
      } else {
        this.$message.warning('请选择需要删除的项目')
      }
    },

    /* table-item 按钮 */
    // 编辑
    handleEdit(data) {
      const obj = { thresholdid: data.thresholdid, thresholdname: data.thresholdname, thresholddescribe: data.thresholddescribe }
      this.dialogFrom = { ...obj }
      this.dislogTitle = '编辑阈值信息'
      this.dialogFormState = 1
      this.dialogFormVisible = true
    },
    // 删除
    handleDelete(data) {
      this.$confirm('此操作将删除当前项目, 是否继续?', '确认操作', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const nowArr = [data.thresholdid]
          const { message } = await deleteThreshold(nowArr)
          if (this.tableData.length === 1) {
            if (this.pages.page > 1) {
              this.pages.page--
            } else {
              this.pages.page = 1
            }
          }
          await this.getThresholdList()
          this.$message.success({ message })
        })
        .catch(error => {
          console.log(error)
        })
    },

    // 弹出框事件
    // 关闭事件
    dialogClose() {
      this.dislogTitle = null
      this.dialogFormState = null
      this.dialogFrom = { thresholdname: null, thresholddescribe: null }
    },
    // 添加
    async dialogSubmit() {
      try {
        const { message } = await addThresholdItme(this.dialogFrom)
        this.$message.success(message)
      } catch (error) {
        console.log(error)
      } finally {
        this.getThresholdList()
        this.dialogFormVisible = false
      }
    },
    // 编辑
    async dialogEditSubmit() {
      try {
        const { message } = await editThresholdItme(this.dialogFrom)
        this.$message.success(message)
      } catch (error) {
        console.log(error)
      } finally {
        this.getThresholdList()
        this.dialogFormVisible = false
      }
    },

    /* 分页栏 */
    // 页码选择事件
    handleCurrentChange(newPage) {
      this.pages.page = newPage
      this.getThresholdList()
    },
    // 表格显示条数选择事件
    handleSizeChange(newSize) {
      this.pages.size = newSize
      this.getThresholdList()
    }
  }
}
</script>

<style scoped>
.el-table {
  margin-top: 10px;
}
.el-form {
  margin-left: 20px;
}
.el-input {
  width: 300px;
}
</style>

<style lang="scss" scoped>
.row1 {
  margin-bottom: 10px;
  .search {
    border: 1px solid #dcdfe6;
    border-top: none;
    padding: 34px 10px 10px 10px;
    margin-top: -14px;
  }
}
.dialogFrom {
  .el-input,
  .el-select,
  .el-textarea,
  .el-cascader {
    width: calc(100% - 10px);
  }
}
</style>
